package com.conan.dailyflickr.provider;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;

public class DailyFlickrProvider extends ContentProvider{
	private final String TAG = "DailyFlickrProvider";
	
	private DailyFlickrDBHelper mOpenHelper;
	
	public static final class DailyFlickr implements BaseColumns{
    	public static final Uri  CONTENT_URI = 
    				Uri.parse("content://" + DailyFlickrDBConstants.AUTHORITY);
    	public static final String CONTENT_TYPE = 
    		"vnd.android.cursor.dir/vnd.handroid.notes";
    	// table column
    }
	
	private static HashMap<String, String> mPhotoProjectionMap;
	private static UriMatcher sUriMatch = new UriMatcher(UriMatcher.NO_MATCH);
	
	static {		
		sUriMatch.addURI(DailyFlickrDBConstants.AUTHORITY,
					DailyFlickrDBConstants.TABLE_PHOTO_NAME, 
					DailyFlickrDBConstants.TABLE_PHOTO_ID);
		
		mPhotoProjectionMap = new HashMap<String, String>();
		for(String column: DailyFlickrDBConstants.mPhotoColumn){
			mPhotoProjectionMap.put(column, column);
		}
	}
	
	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		// Log.d(TAG, "delete() with uri = " + uri);
		
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		
		int count = 0;
		
		switch (sUriMatch.match(uri)) {
		case DailyFlickrDBConstants.TABLE_PHOTO_ID:
			count = db.delete(DailyFlickrDBConstants.TABLE_PHOTO_NAME, where, whereArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(Uri uri) {
		switch (sUriMatch.match(uri)) {
		case DailyFlickrDBConstants.TABLE_PHOTO_ID:
			return DailyFlickr.CONTENT_TYPE;

		default:
			throw new IllegalArgumentException("Unkown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues initValues) {
		if(sUriMatch.match(uri) != DailyFlickrDBConstants.TABLE_PHOTO_ID){
			throw new IllegalArgumentException("Unkown URI " + uri); 
		}
		
		ContentValues values;
		if(initValues != null){
			values = new ContentValues(initValues);
		}else{
			values = new ContentValues();
		}
		
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		long rowId = db.insert(DailyFlickrDBConstants.TABLE_PHOTO_NAME, 
									DailyFlickrDBConstants.mPhotoColumn[0], values);
		if(rowId > 0){
			Uri photoUri = ContentUris.withAppendedId(DailyFlickr.CONTENT_URI, rowId);
			getContext().getContentResolver().notifyChange(photoUri, null);
			return photoUri;
		}
		
		throw new SQLException("failed to insert row into " + uri);
	}

	@Override
	public boolean onCreate() {
		mOpenHelper = new DailyFlickrDBHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		
		switch (sUriMatch.match(uri)) {
		case DailyFlickrDBConstants.TABLE_PHOTO_ID:
			queryBuilder.setTables(DailyFlickrDBConstants.TABLE_PHOTO_NAME);
			queryBuilder.setProjectionMap(mPhotoProjectionMap);
			break;

		default:
			throw new IllegalArgumentException("Unknow uri " + uri); 
		}
		
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null , sortOrder);
		
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count;
		switch (sUriMatch.match(uri)) {
		case DailyFlickrDBConstants.TABLE_PHOTO_ID:
			count = db.update(DailyFlickrDBConstants.TABLE_PHOTO_NAME, values, where, whereArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URI " + uri);			
		}
		
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	/*****************************************************************
	 ********************	DailyFlickrDBHelper	**********************
	 *****************************************************************/
	public class DailyFlickrDBHelper extends SQLiteOpenHelper{

		
		
		public DailyFlickrDBHelper(Context context) {
			super(context, DailyFlickrDBConstants.DATABASE_NAME, null, DailyFlickrDBConstants.DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			for(String table: DailyFlickrDBConstants.mSqlCreateTable){
				db.execSQL(table);
			}		
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			int version = oldVersion;
			
			switch (version) {
			case DailyFlickrDBConstants.VER_ORIGINAL:
				// for(String table: DailyFlickrDBConstants.mTableNames){
					// db.execSQL("ALTER TABLE " + table);
				// }
				break;
			}
			
			if(version != DailyFlickrDBConstants.DATABASE_VERSION){
				// destroying old data during upgrade
				
				db.execSQL("DROP TABLE IF EXISTS " + DailyFlickrDBConstants.TABLE_PHOTO_NAME);				
				
				onCreate(db);
			}
		}
	}
}


